/*
 * Project it.battlehorse.rcp.tools.dbbase
 * Created on Nov 21, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.dbbase.preferences;

import it.battlehorse.rcp.sl.annot.Injected;
import it.battlehorse.rcp.sl.annot.Serviceable;
import it.battlehorse.rcp.tools.dbbase.DatasourceConfig;
import it.battlehorse.rcp.tools.dbbase.DbBaseActivator;
import it.battlehorse.rcp.tools.dbbase.IDbManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 * Defines the content provider for the table which contains the
 * Datasource configurations 
 * 
 * @author battlehorse
 * @since Nov 21, 2005
 */
@Serviceable
public class DbTableContentProvider implements IStructuredContentProvider {
	
	/* a database manager */
	private IDbManager dbManager;
	
	/**
	 * Injects a db manager into this object
	 * 
	 * @param manager a db manager
	 */
	@Injected
	public void setDbManager(IDbManager manager) {
		this.dbManager = manager;
	}	
	
	private DbPreferences viewer;	
	
	private List<DatasourceConfig> sources;
	
	/**
	 * Creates a new content provider loading the available datasources from the 
	 * given preferences store.
	 * 
	 * @param preferences the preferences store which contains the list of available datasources.
	 */
	public DbTableContentProvider(DbPreferences preferences) {
		this.viewer = preferences;
		IPreferenceStore store = DbBaseActivator.getDefault().getPreferenceStore();
		sources =  dbManager.loadDatasourceConfig(store);
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
	 */
	public Object[] getElements(Object inputElement) {
		return sources.toArray();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	public void dispose() {
		viewer = null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// nothing to do
	}
	
	/**
	 * Adds or edits an existing datasource configuration and refreshes the viewer
	 * associated to this content provider
	 * 
	 * @param dsConfig the datasource config to be added (or edited)
	 * @param edit {@code true} if the datasource is to be edited, {@code false} if
	 * 	the datasource is being added. 
	 */
	public void addDatasourceConfig(DatasourceConfig dsConfig, boolean edit) {
		if (!edit) {		
			sources.add(dsConfig);	// we assert that the dsConfig name is unique.
		}
		else {
			int dsPos = -1;
			for ( int pos = 0 ; pos < sources.size() ; pos++) {
				if (sources.get(pos).equals(dsConfig)) {
					dsPos = pos;
					break;
				}
					
			}
			if (dsPos != -1)		
				sources.set(dsPos, dsConfig);
		}
		
		viewer.refresh(dsConfig);
	}	
	
	/**
	 * Removes the given datasource configuration from this content provider
	 * 
	 * @param dsConfig the datasource to be removed.
	 */
	public void removeDatasourceConfig(DatasourceConfig dsConfig) {
		Iterator<DatasourceConfig> it = sources.iterator();
		while (it.hasNext()) {
			DatasourceConfig cfg = it.next();	
			if (cfg.equals(dsConfig))
				it.remove();
		}
		
		viewer.refresh(null);
	}
	
	/**
	 * Returns the list of the current datasource names.
	 * 
	 * @return the list of the current datasource names
	 */
	public List<String> getSourceNames() {
		List<String> res = new ArrayList<String>();
		for (DatasourceConfig c : sources) {
			res.add(c.getName());
		}
		
		return res;
	}

	/**
	 * Returns the datasource configurations
	 * 
	 * @return Returns the datasource configurations
	 */
	public List<DatasourceConfig> getSources() {
		return sources;
	}
	
	

}
